package com.cloud.admin.controller.tool;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baidu.ueditor.ActionEnter;
import com.cloud.admin.config.AliOssConfig;
import com.cloud.admin.config.UeditorConfig;
import com.cloud.common.oss.AliOssRequest;
import com.cloud.common.oss.AliOssResponse;
import com.cloud.common.oss.AliUploadHelper;
import com.cloud.common.util.R;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @author mags
 * @since 2019/4/15 10:23
 */
@Controller
@AllArgsConstructor
@RequestMapping("/tool/ueditor")
public class UeditorController {
    private final AliOssConfig aliOssConfig;

    //    private final QiNiuConfig qiNiuConfig;
//  TODO  static/bootstrap/ueditor/jsp/config.json 该路径加gxcc，本项目不能修改    , serverUrl: server_url+"/gxcc/ueditor/jsp/config"
    @ResponseBody
    @RequestMapping("/config")
    public Object config(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String action = request.getParameter("action");

        if (action != null && action.equals("uploadimage")) {
            MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
            MultipartFile multipartFile = multipartHttpServletRequest.getFile("upfile");
            R r = uploadImg(multipartFile, request);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("original", multipartFile.getOriginalFilename());
            map.put("name", multipartFile.getOriginalFilename());
            map.put("url", r.getData());
            map.put("size", multipartFile.getSize());
            map.put("type", "." + multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().indexOf(".")));
            map.put("state", "SUCCESS");
            return map;
        } else {
            return UeditorConfig.getConfig();
        }
    }

    /**
     * 上传文件到OSS云存储
     *
     * @param multipartFile
     * @return
     * @throws IOException
     */
    private R uploadImg(MultipartFile multipartFile, HttpServletRequest request) throws IOException {
        String filedir = "ueditor/" + DateUtil.today().replace("-", "/") + "/" + cn.hutool.core.lang.UUID.randomUUID().toString() + ".jpg";
        FileInputStream inputStream = (FileInputStream) multipartFile.getInputStream();
        AliOssRequest aliOssRequest = new AliOssRequest();
        BeanUtil.copyProperties(aliOssConfig, aliOssRequest);

        AliUploadHelper aliUploadHelper = new AliUploadHelper(aliOssRequest);
        AliOssResponse aliOssResponse =
                aliUploadHelper.uploadSimple(inputStream, filedir);
        return new R<>(aliOssResponse.getAbsolutePath());
    }

}
